<!DOCTYPE html>
<html class="ui-page-login">

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>注册</title>
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/main.css" />
		<link href="../css/mui.picker.css" rel="stylesheet" />
		<link href="../css/mui.poppicker.css" rel="stylesheet" />
		<style>
			.mui-input-group:first-child {
				margin-top: 20px;
			}
			
			.mui-input-group label {
				width: 22%;
			}
			
			.mui-input-row label~input,
			.mui-input-row label~select,
			.mui-input-row label~textarea {
				width: 78%;
				height: 0.9rem;
				font-size: 14px;
			}
			
			.mui-checkbox input[type=checkbox],
			.mui-radio input[type=radio] {
				top: 6px;
			}
			
			.mui-content-padded {
				margin-top: 25px;
			}
			
			#reg {
				padding: 10px;
				border-radius: 20px;
				width: 100%;
				margin: 0 auto;
				height: 0.9rem;
			}
			
			#reg span {
				font-size: 18px;
			}
			
			.mui-input-group {
				background: none;
			}
			
			.mui-input-group:before,
			.mui-input-group:after {
				height: 0;
			}
			
			.mui-input-group input {
				background-color: #FFFFFF !important;
				width: 95% !important;
				border-radius: 25px !important;
				margin: 10px 0 10px 0 !important;
				padding-left: 55px !important;
				float: inherit !important;
			}
			
			.mui-input-group .mui-input-row:after {
				height: 0;
			}
			
			.mui-input-group .mui-input-row {
				text-align: center;
			}
			
			.mui-input-group .mui-input-row {
				height: inherit;
			}
			
			p {
				font-size: 14px;
				margin-top: 20px;
				margin-bottom: 10px;
				color: #8f8f94;
			}
			
			p a {
				color: #000000;
				padding: 0 20px;
			}
			
			.mui-content {
				background: none;
			}
			
			#showUserPicker {
				float: inherit;
				width: 87%;
				padding: 10px 15px;
				background-color: #FFF !important;
			}
			
			#yanzhengBtn {
				position: absolute;
				top: 0.35rem;
				right: 0.35rem;
				background: #ffcc99;
				border: 0;
				border-radius: 20px;
			}
			
			.mui-input-group .labelL {
				position: absolute;
				top: 5px;
				left: -5px;
				z-index: 99;
			}
			
			.mui-input-group .labelL img {
				width: 0.69rem;
				vertical-align: -webkit-baseline-middle;
			}
			
			.mui-input-group .labelR {
				position: absolute;
				top: 10px;
				right: 0px;
				z-index: 99;
			}
			
			.mui-input-group .labelR img {
				width: 0.3rem;
				vertical-align: -webkit-baseline-middle;
			}
			
			html,
			body {
				height: 100%;
				margin: 0px;
				padding: 0px;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">注册</h1>
		</header>
		<div class="mui-content">
			<div class="mui-input-group">
				<div class="mui-input-row">
					<label class="labelL"><img src="../images/exit/dl.png" /></label>
					<input id='account' name='account' type="number" class="mui-input" placeholder="请输入您的手机号码">
				</div>
				<div class="mui-input-row">
					<label class="labelL"><img src="../images/exit/yzm.png" /></label>
					<input id='password' name='password' type="number" class="mui-input password" placeholder="请输入验证码"><button type="button" class="verification size12" id="yanzhengBtn">获取验证码</button></input>
				</div>

				<div class="mui-input-row">
					<label class="mui-pull-right labelL"><img src="../images/exit/yy.png" /></label>
					<input id='showUserPicker' class="mui-navigate-right" type='text' placeholder="注册来源" readonly="readonly"></input>
					<!--<button id='showUserPicker' class="mui-btn mui-btn-block" type='button'>选择医院</button>-->
					<label class="labelR" id='showUserPicker_img'><img src="../images/exit/xz.png" /></label>
				</div>
				<div class="mui-input-row">
					<label class="mui-pull-right labelL"><img src="../images/exit/mm.png" /></label>
					<input id='setpassword' name='setpassword' type="password" class="mui-input password" placeholder="设置密码"></input>
				</div>
				<div class="mui-input-row">
					<label class="mui-pull-right labelL"><img src="../images/exit/mm.png" /></label>
					<input id='doublepassword' name='doublepassword' type="password" class="mui-input password" placeholder="重复密码"></input>
				</div>
			</div>
			<div class="mui-content-padded  zhiqing">
				<p>
					<input id="register_user" name="style" type="checkbox" value="slider" class="chk_1">
					<label for="register_user"></label>
					<span class="size12">已阅读并同意<span id="user_document">《用户协议书》</span></span>
				</p>
			</div>
			<div class="mui-content-padded">
				<button id='reg' class="mui-btn mui-btn-block mui-btn-primary mui-disabled"><span>保存</span></button>
			</div>

		</div>
		<script src="../js/mui.min.js"></script>
		<script type="text/javascript" src="../js/xiangyingshi.js"></script>
		<script src="../js/app.js"></script>
		<script type="text/javascript" src="../js/constants.js"></script>
		<script src="../js/mui.picker.js"></script>
		<script src="../js/mui.poppicker.js"></script>
		<script type="text/javascript" src="../plugs/moment/moment.min.js"></script>
		<script type="text/javascript" src="../js/json_time.js"></script>
		<script type="text/javascript" src="../js/jquery-3.1.1.js"></script>
		<script>
			var hospital = '';
			var hospitalText = '';
			mui.plusReady(function() {
				if(plus.navigator.isImmersedStatusbar()) { // 兼容immersed状态栏模式
					// 获取状态栏高度并根据业务需求处理，这里重新计算了子窗口的偏移位置
					topoffset = (Math.round(plus.navigator.getStatusbarHeight()) + 45) + 'px';
					document.querySelector("header").style.height = topoffset;
					document.querySelector("header").style.paddingTop = "20px";
					document.querySelector(".mui-bar-nav~.mui-content").style.paddingTop = topoffset;
				}

				//				plus.webview.currentWebview().setStyle({
				//					softinputMode: "adjustResize" // 弹出软键盘时自动改变webview的高度
				//				});
				var currentView = plus.webview.currentWebview();
				currentView.show('slide-in-right', 200);
				plus.nativeUI.closeWaiting();

				//知情同意书勾选
				$('#register_user').click(function(e) {
					if($('#register_user').is(':checked')) {
						$("#reg").removeClass("mui-disabled");
					} else {
						$("#reg").addClass("mui-disabled");
					}
				})
				//倒计时
				var past_time = 0,
					begin_time = localStorage.getItem("begin_time"),
					daojishi_current = localStorage.getItem("miao");
				var daojishi2 = 60;
				if(daojishi_current != null && begin_time != null) {
					//获取存在本地的退出时间
					daojishi_current = localStorage.getItem("miao");
					//获取最开始时间
					begin_time = localStorage.getItem("begin_time");
					past_time = date_Time_minus(begin_time, daojishi_current).split(":")[0] * 60 + parseInt(date_Time_minus(begin_time, daojishi_current).split(":")[1]);
					daojishi2 = 60 - past_time;
					var daojishi_interval = setInterval(function() {
						if(daojishi2 > 0) {
							document.querySelector('#yanzhengBtn').innerHTML = daojishi2 + "秒";
							document.querySelector('#yanzhengBtn').setAttribute("class", "verification size12 mui-disabled");
							daojishi2--;
							localStorage.setItem("miao", date_All_format());
						} else {
							document.querySelector('#yanzhengBtn').innerHTML = "获取验证码";
							document.querySelector('#yanzhengBtn').setAttribute("class", "verification size12");
							document.querySelector("#reg").setAttribute("class", "mui-btn mui-btn-block mui-btn-primary");
							clearInterval(daojishi_interval);
							localStorage.clear();
							daojishi2 = 60;
							past_time = 0;
						}
					}, 1000);
				}
				//发送请求按钮的点击事件
				document.getElementById("yanzhengBtn").addEventListener('tap', function() {
					// 关闭软键盘
					document.activeElement.blur();
					var phone = document.getElementById("account").value;
					var phone_validate = /1[3|5|7|8|]\d{9}/; //验证手机号码是否正确

					time_current = date_All_format();
					localStorage.setItem("begin_time", date_All_format());
					if(phone == "") {
						mui.toast('请填写手机号')
						return false;
					}
					if(phone.length != 11) {
						mui.toast('手机号码位数不正确');
						return false;
					}
					if(!phone_validate.test(phone)) {
						mui.toast('手机号码错误');
						return false;
					}

					document.querySelector('#yanzhengBtn').setAttribute("class", "verification size12 mui-disabled");

					var url = serverAddress + "/api/patient/appsendRegVerCode";
					mui.ajax(url, {
						data: {
							pn: accountBox.value,
							uid: plus.push.getClientInfo().clientid
						},
						dataType: 'json', //服务器返回json格式数据
						type: 'get', //HTTP请求类型
						timeout: 180000, //超时时间设置为30秒；
						headers: {
							/*'Content-Type': 'application/json'*/
						},
						success: function(data) {
							//服务器返回响应，根据响应结果，分析是否登录成功；
							if(data.result == "sendfailed") {
								mui.toast("验证码发送失败");
								document.querySelector('#yanzhengBtn').setAttribute("class", "verification size12");
								return false;
							}
							if(data.result == "success") {
								localStorage.setItem('TOKEN', accountBox.value);
								var daojishi_interval = setInterval(function() {
									if(daojishi2 > 0) {
										document.querySelector('#yanzhengBtn').innerHTML = daojishi2 + "秒";
										document.querySelector('#yanzhengBtn').setAttribute("class", "verification size12 mui-disabled");
										daojishi2--;
										localStorage.setItem("miao", date_All_format());

									} else {
										document.querySelector('#yanzhengBtn').innerHTML = "获取验证码";
										document.querySelector('#yanzhengBtn').setAttribute("class", "verification size12");
										document.querySelector("#reg").setAttribute("class", "mui-btn mui-btn-block mui-btn-primary");
										localStorage.clear();
										clearInterval(daojishi_interval);
										past_time = 0;
										daojishi2 = 60;
									}
								}, 1000);
							}
							mui.toast("发送成功");
						},
						error: function(xhr, type, errorThrown) {
							//异常处理；
							mui.toast('验证码网络超时，请稍后再试！');
							plus.nativeUI.closeWaiting();
						}
					});
				});

				//获取医院列表

				//				var url = serverAddress + "/api/patient/gethosnames";
				//				
				//				var success = function(data) {
				//					console.log("获取医生ID"+JSON.stringify(data));
				//					userPicker.setData(data);
				//				};
				//commonHttpUtils(url, "get", {}, success, error, true);

				var showUserPicker = document.getElementById("showUserPicker");
				var showUserPickerButton = document.getElementById('showUserPicker_img');
				var userResult = document.getElementById('userResult');
				showUserPickerButton.addEventListener("tap", function(event) {
					var userPicker = new mui.PopPicker();
					userPicker.setData([{
						value: '现场活动',
						text: '现场活动'
					}, {
						value: '网络获取',
						text: '网络获取'
					}, {
						value: '朋友介绍',
						text: '朋友介绍'
					}]);
					userPicker.show(function(items) {
						showUserPicker.value = items[0].text;
						hospitalText = items[0].text;
						hospital = items[0].value;
						//返回 false 可以阻止选择框的关闭
						//return false;
						userPicker.dispose();
					});

				}, false);
				//-----------------------------------------

				var settings = app.getSettings();
				var regButton = document.getElementById('reg');
				var accountBox = document.getElementById('account');
				var passwordBox = document.getElementById('password');
				var userID = "";
				regButton.addEventListener('tap', function(event) {
					$("#reg").addClass("mui-disabled");
					document.activeElement.blur();
					var phone = document.getElementById("account").value.trim();
					var password = document.getElementById("password").value;
					var setpassword = document.getElementById("setpassword").value;
					var doublepassword = document.getElementById("doublepassword").value;
					var phone_validate = /1[3|5|7|8|]\d{9}/; //验证手机号码是否正确
					if(phone == "") {
						mui.toast('请填写手机号');
						plus.nativeUI.closeWaiting();
						return false;
					}
					if(phone.length != 11) {
						mui.toast('手机号码位数不正确');
						return false;
					}
					if(!phone_validate.test(phone)) {
						mui.toast('手机号码错误');
						return false;
					}
					if(password == "") {
						mui.toast('请填写验证码');
						plus.nativeUI.closeWaiting();
						return false;
					}
					if(hospital == "") {
						mui.toast("请选择医院");
						return false;
					}
					if(setpassword == "" || doublepassword == "") {
						mui.toast("请填写密码");
						return false;
					} else if(setpassword != doublepassword) {
						mui.toast("两次密码填写不一致");
						return false;
					}
					var regInfo = {
						account: accountBox.value,
						Validation_password: passwordBox.value,
						CLIENTID: plus.push.getClientInfo().clientid,
						password: setpassword,
						EXTENSIONFROM: hospital
					};
					//console.log(JSON.stringify(regInfo));
					//plus.webview.getLaunchWebview().show("pop-in", 200, function() {
					//	plus.webview.currentWebview().close("none");
					//});
					//若启动页不是登录页，则需通过如下方式打开登录页

					var url = serverAddress + "/api/patient/appregister";
					mui.ajax(url, {
						data: regInfo,
						dataType: 'json', //服务器返回json格式数据
						type: 'post', //HTTP请求类型
						timeout: 180000, //超时时间设置为30秒；
						async: false,
						beforeSend: function() {
							plus.nativeUI.showWaiting();
						},
						complete: function() {
							plus.nativeUI.closeWaiting();
						},
						success: function(data) {
							userID = data.id;
							console.log("1" + JSON.stringify(data));
							//服务器返回响应，根据响应结果，分析是否登录成功；
							if(data.result == "success") {
								
							//云通讯sdk的
							var sdkurl = serverAddress + "/api/sdk/regAndGetsign";
							var sdkdata = {
								userID: data.id,
								userName:data.id,
								type:"patient"
							};
							var sdksussess = function(data) {
								console.log("109090data==>"+JSON.stringify(data));
								localStorage.setItem("adminID", data.adminID);
								localStorage.setItem("adminSig", data.adminSig);
								localStorage.setItem("userID", data.userID);
								localStorage.setItem("userSig", data.userSig);
							}
							commonHttpUtils(sdkurl, "post", sdkdata, sdksussess, error, false);
							
								localStorage.setItem('TOKENID', data.id);
								localStorage.setItem("hospital", hospitalText);
								localStorage.setItem("num", '1');
								var loginInfo = {
									PHONE: accountBox.value,
									CODE: setpassword,
									IMEI_PHONE: plus.device.uuid,
									CLIENT_ID: plus.push.getClientInfo().clientid,
									os: plus.os.name
								};
								var successLogin = function(data) {
									if(data.result == "success") {
										
										localStorage.setItem('TOKEN', accountBox.value);
										localStorage.setItem('TOKENMD5', data.access_token);
										localStorage.setItem("TOKENID", data.id);

										//获取是否实名认证，如果没有实名认证进入实名认证页面
										//判断第几次登录
										if(localStorage.getItem("num") == "1") {
											mui.toast("您没有实名认证！");
											mui.openWindow({
												url: "../geren_message/d02_geren_message.html",
												id: "d02_geren_message.html"
											});
											localStorage.removeItem("num");
											return false;
										}

										var index = plus.webview.getLaunchWebview();
										index.reload(true);
										plus.webview.currentWebview().close("none");
									}
								};
								var url = serverAddress + "/api/patient/applogin";
								commonHttpUtils(url, "post", loginInfo, successLogin, error, false);
							}
							if(data.result == "verCodeKey is wrong") {
								mui.toast("验证码错误");
								plus.nativeUI.closeWaiting();
								return false;
							}
							if(data.result == "exist") {
								mui.toast("手机号已经注册过,请登录");
								mui.back();
							}
						},
						error: function(xhr, type, errorThrown) {
							//异常处理；
							mui.toast('注册网络超时，请稍后再试！');
							plus.nativeUI.closeWaiting();
						}
					});
				});

				document.getElementById("user_document").addEventListener("tap", function(e) {
					mui.openWindow({
						url: '../document/e03_insurance_agreement.html',
						id: 'e03_insurance_agreement.html',
						show: {
							autoShow: false, //页面loaded事件发生后自动显示，默认为true
							event: 'loaded' //页面显示时机，默认为titleUpdate事件时显示
						},
						waiting: {
							autoShow: true //自动显示等待框，默认为true
						}
					});
				});
			});
			
		</script>
	</body>

</html>